package com.congwulin.wechat.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.congwulin.wechat.entity.common.SuperVO;

import java.util.*;

public class BillTransUtil {
    public static ArrayList<Map<Class<?>,List<SuperVO>>> ObjToEntity(Object obj, Map<String, Class<?>> map){
        if(obj instanceof ArrayList){
            return transArray((ArrayList<LinkedHashMap>)obj, map);
        }else if(obj instanceof LinkedHashMap){
            ArrayList<LinkedHashMap> list = new ArrayList<LinkedHashMap>();
            list.add((LinkedHashMap)obj);
            return transArray(list, map);
        }else{
            return null;
        }
    }

    public static ArrayList<Map<Class<?>,List<SuperVO>>> transArray(ArrayList<LinkedHashMap> list, Map<String, Class<?>> map){
        ArrayList<Map<Class<?>,List<SuperVO>>> areaList = new ArrayList<Map<Class<?>,List<SuperVO>>>();
        Map<Class<?>,List<SuperVO>> voMap = null;
        List<SuperVO> listVO = null;
        for(LinkedHashMap linkedHashMap: list){
            voMap = new HashMap<Class<?>,List<SuperVO>>();
            for(Object area: linkedHashMap.keySet()){
                if(linkedHashMap.get(area) == null){
                    ExceptionUtils.wrapBusinessException("单据数据格式错误！！！");
                }else if(linkedHashMap.get(area) instanceof ArrayList){
                    String arrayString = JSONArray.toJSONString(linkedHashMap.get(area));
                    listVO = (List<SuperVO>) JSONObject.parseArray(arrayString, map.get(area));
                }else{
                    listVO = new ArrayList<SuperVO>();
//                    System.out.println(linkedHashMap.get(area).toString());
                    String singleString = JSONObject.toJSONString(linkedHashMap.get(area));
                    listVO.add((SuperVO)JSONObject.parseObject(singleString,map.get(area)));
                }
                voMap.put(map.get(area),listVO);
            }
            areaList.add(voMap);
        }
        return areaList;
    }
}
